package cn.stylefeng.guns.modular.fi.service.impl;

import cn.hutool.core.collection.ListUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.fi.entity.FiFreightDetails;
import cn.stylefeng.guns.modular.fi.entity.FiFreightDetailsLog;
import cn.stylefeng.guns.modular.fi.mapper.FiFreightDetailsMapper;
import cn.stylefeng.guns.modular.fi.model.params.FiFreightDetailsParam;
import cn.stylefeng.guns.modular.fi.model.result.FiFreightDetailsResult;
import cn.stylefeng.guns.modular.fi.service.FiFreightDetailsLogService;
import  cn.stylefeng.guns.modular.fi.service.FiFreightDetailsService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangzh
 * @since 2024-11-04
 */
@Service
public class FiFreightDetailsServiceImpl extends ServiceImpl<FiFreightDetailsMapper, FiFreightDetails> implements FiFreightDetailsService {

    @Autowired
    private FiFreightDetailsLogService fiFreightDetailsLogService;

    @Autowired
    private UserService userService;

    @Override
    public void add(FiFreightDetailsParam param){
        FiFreightDetails entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(FiFreightDetailsParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(FiFreightDetailsParam param){
        FiFreightDetails oldEntity = getOldEntity(param);
        FiFreightDetails newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    /**
     * 修改运费
     * @param param
     * @return
     */
    @Override
    public ResponseData updateCalcCost(FiFreightDetailsParam param){
        FiFreightDetails oldEntity = getOldEntity(param);
        FiFreightDetails newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        newEntity.setUntaxAmt(newEntity.getCalcCost().divide(BigDecimal.ONE.add(oldEntity.getTaxRate()),3, BigDecimal.ROUND_HALF_DOWN));
        newEntity.setTaxAmt(newEntity.getCalcCost().subtract(newEntity.getUntaxAmt()));
        this.updateById(newEntity);
        this.baseMapper.updateDispatch(oldEntity.getDispatchNumber());

        return ResponseData.success();
    }


    @Override
    public FiFreightDetailsResult findBySpec(FiFreightDetailsParam param){
        return null;
    }

    @Override
    public List<FiFreightDetailsResult> findListBySpec(FiFreightDetailsParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(FiFreightDetailsParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<FiFreightDetailsResult> listAll(FiFreightDetailsParam param, String condition) {
        return this.baseMapper.listAll(param,condition);
    }

    @Override
    public int delByDispatchNumber(List<String> dispatchNumber) {
        return this.baseMapper.delByDispatchNumber(dispatchNumber);
    }

    @Override
    @Transactional(rollbackFor = Exception.class,timeout = 60)
    public ResponseData shenhe(String ids) {
        if (ToolUtil.isEmpty(ids)) {
            return ResponseData.error("请选择【待审核】的数据");
        }
        User user = userService.getcurrentUser();
        List<FiFreightDetailsLog> fiFreightDetailsLogList = new ArrayList<>();
        FiFreightDetailsLog fiFreightDetailsLog = null;
        List<String> list = ListUtil.of(ids.split(","));
        List<FiFreightDetails> fiFreightDetails = this.lambdaQuery().in(FiFreightDetails::getId, list).list();
        for(FiFreightDetails obj : fiFreightDetails){
            if(!"待审核".equals(obj.getProcessStatus())){
                return ResponseData.error("请选择【待审核】的数据");
            }
            fiFreightDetailsLog = new FiFreightDetailsLog();
            fiFreightDetailsLog.setDetailsId(obj.getId());
            fiFreightDetailsLog.setDispatchNumber(obj.getDispatchNumber());
            fiFreightDetailsLog.setOpUser(user.getAccount());
            fiFreightDetailsLog.setOpUserName(user.getName());
            fiFreightDetailsLog.setOpTyp("财务审核");
            fiFreightDetailsLog.setOpRemark("财务已审核");
            fiFreightDetailsLogList.add(fiFreightDetailsLog);
        }
        //添加操作日志
        this.fiFreightDetailsLogService.saveBatch(fiFreightDetailsLogList);
        //批量修改状态
        this.lambdaUpdate()
                .in(FiFreightDetails::getId, list)
                .set(FiFreightDetails::getProcessStatus,"已审核")
                .update();
        return ResponseData.success();
    }


    @Override
    @Transactional(rollbackFor = Exception.class,timeout = 60)
    public ResponseData queren(String ids) {
        if (ToolUtil.isEmpty(ids)) {
            return ResponseData.error("请选择【已推送】的数据");
        }
        User user = userService.getcurrentUser();
        List<FiFreightDetailsLog> fiFreightDetailsLogList = new ArrayList<>();
        FiFreightDetailsLog fiFreightDetailsLog = null;
        List<String> list = ListUtil.of(ids.split(","));
        List<FiFreightDetails> fiFreightDetails = this.lambdaQuery().in(FiFreightDetails::getId, list).list();
        for(FiFreightDetails obj : fiFreightDetails){
            if(!"已推送".equals(obj.getProcessStatus())){
                return ResponseData.error("请选择【已推送】的数据");
            }
            fiFreightDetailsLog = new FiFreightDetailsLog();
            fiFreightDetailsLog.setDetailsId(obj.getId());
            fiFreightDetailsLog.setDispatchNumber(obj.getDispatchNumber());
            fiFreightDetailsLog.setOpUser(user.getAccount());
            fiFreightDetailsLog.setOpUserName(user.getName());
            fiFreightDetailsLog.setOpTyp("承运商确认");
            fiFreightDetailsLog.setOpRemark("承运商已确认");
            fiFreightDetailsLogList.add(fiFreightDetailsLog);
        }
        //添加操作日志
        this.fiFreightDetailsLogService.saveBatch(fiFreightDetailsLogList);
        //批量修改状态
        this.lambdaUpdate()
                .in(FiFreightDetails::getId, list)
                .set(FiFreightDetails::getProcessStatus,"已确认")
                .update();
        return ResponseData.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class,timeout = 60)
    public ResponseData shensu(FiFreightDetailsParam fiFreightDetailsParam) {
        User user = userService.getcurrentUser();
        FiFreightDetailsLog fiFreightDetailsLog = new FiFreightDetailsLog();
        fiFreightDetailsLog.setDetailsId(fiFreightDetailsParam.getId());
        fiFreightDetailsLog.setOpUser(user.getAccount());
        fiFreightDetailsLog.setOpUserName(user.getName());
        fiFreightDetailsLog.setOpTyp("承运商申诉");
        fiFreightDetailsLog.setOpRemark(fiFreightDetailsParam.getRemarks());
        //添加操作日志
        this.fiFreightDetailsLogService.save(fiFreightDetailsLog);
        //批量修改状态
        this.lambdaUpdate()
                .eq(FiFreightDetails::getId, fiFreightDetailsParam.getId())
                .set(FiFreightDetails::getProcessStatus,"已申诉")
                .update();
        return ResponseData.success();
    }



    @Override
    @Transactional(rollbackFor = Exception.class,timeout = 60)
    public ResponseData tuisong(String ids,String carrierUserCode) {
        if (ToolUtil.isEmpty(ids)) {
            return ResponseData.error("请选择要推送的数据");
        }
        User user = userService.getcurrentUser();
        List<FiFreightDetailsLog> fiFreightDetailsLogList = new ArrayList<>();
        FiFreightDetailsLog fiFreightDetailsLog = null;
        List<String> list = ListUtil.of(ids.split(","));
        List<FiFreightDetails> fiFreightDetails = this.lambdaQuery().in(FiFreightDetails::getId, list).list();
        for(FiFreightDetails obj : fiFreightDetails){
            if(!("已审核".equals(obj.getProcessStatus()) || "已申诉".equals(obj.getProcessStatus()))){
                return ResponseData.error("请选择【已审核】或【已申诉】的数据");
            }
            fiFreightDetailsLog = new FiFreightDetailsLog();
            fiFreightDetailsLog.setDetailsId(obj.getId());
            fiFreightDetailsLog.setDispatchNumber(obj.getDispatchNumber());
            fiFreightDetailsLog.setOpUser(user.getAccount());
            fiFreightDetailsLog.setOpUserName(user.getName());
            fiFreightDetailsLog.setOpTyp("财务推送");
            if("已审核".equals(obj.getProcessStatus()) ){
                fiFreightDetailsLog.setOpRemark("审核后推送");
            }else if("已申诉".equals(obj.getProcessStatus()) ){
                fiFreightDetailsLog.setOpRemark("申诉后推送");
            }
            fiFreightDetailsLogList.add(fiFreightDetailsLog);
        }
        //添加操作日志
        this.fiFreightDetailsLogService.saveBatch(fiFreightDetailsLogList);
        //批量修改状态
        this.lambdaUpdate()
                .in(FiFreightDetails::getId, list)
                .set(FiFreightDetails::getProcessStatus,"已推送")
                .set(FiFreightDetails::getCarrierUserCode,carrierUserCode)
                .update();
        return ResponseData.success();
    }

    private Serializable getKey(FiFreightDetailsParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private FiFreightDetails getOldEntity(FiFreightDetailsParam param) {
        return this.getById(getKey(param));
    }

    private FiFreightDetails getEntity(FiFreightDetailsParam param) {
        FiFreightDetails entity = new FiFreightDetails();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
